//
//  FilterOptionView.m
//  CloudMD
//

//  Copyright © 2016年 HuaZhengInfo. All rights reserved.
//

#import "FilterOptionView.h"
#import "AWOptionButton.h"

@implementation FilterOptionView

-(void)addButtonWithViewWidth:(float)viewWidth array:(NSArray *)array{
    self.layer.masksToBounds=YES;
    self.buttonMutableArray=[[NSMutableArray alloc] init];
    self.buttonSelectedArray=[[NSMutableArray alloc] init];
    self.rowNum=1;
    
    
    CGPoint point=CGPointZero;
    CGSize tmp = CGSizeZero;
    for (int i=0; i<[array count]; i++) {
        CGSize needSize = [array[i] boundingRectWithSize:CGSizeMake(viewWidth, 27)//改
                                                 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                              attributes:@{
                                                           NSFontAttributeName: [UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor blackColor]
                                                           }
                                                 context:nil].size;
        
        point=CGPointMake(point.x+HorizontalInterverl+tmp.width, point.y);
        //为保证这种类型的按钮宽度与使用上面方法的保持一致。使用一行4（上面的方法的num一般使用值）个为基准，设立最小宽度。
        float minWidth=(viewWidth-HorizontalInterverl*4.0)/4.0;
        
        CGSize size={
            .width=needSize.width<(minWidth-10)?minWidth:needSize.width+10,
            .height=27,//改
        };
        
        tmp = size;
        
        if (point.x+HorizontalInterverl+size.width>viewWidth) {
            point=CGPointMake(HorizontalInterverl, point.y+VerticalInterverl+27);//改
            self.rowNum++;
        }
        if(point.x==HorizontalInterverl){
            point=CGPointMake(0, point.y);
        }
        
        AWOptionButton *button=[[AWOptionButton alloc] initWithFrame:CGRectMake(point.x,point.y, size.width, size.height)];
        
        [self.buttonMutableArray addObject:button];
        [self.buttonSelectedArray addObject:@0];
        // button.layer.borderWidth=0.5;
        button.titleLabel.font=[UIFont systemFontOfSize:13];
        [self setOptionButtonUnselected2:button];//改
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.tag=i;
        [button addTarget:self action:@selector(optionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        self.sumHeight=CGRectGetMaxY(button.frame);
    }
}
-(void)zhaoaddButtonWithViewWidth:(float)viewWidth array:(NSArray *)array{
    self.layer.masksToBounds=YES;
    self.buttonMutableArray=[[NSMutableArray alloc] init];
    self.buttonSelectedArray=[[NSMutableArray alloc] init];
    self.rowNum=1;
    
    
    CGPoint point=CGPointZero;
    CGSize tmp = CGSizeZero;
    for (int i=0; i<[array count]; i++) {
        CGSize needSize = [array[i] boundingRectWithSize:CGSizeMake(viewWidth, 27)//改
                                                 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                              attributes:@{
                                                           NSFontAttributeName: [UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor blackColor]
                                                           }
                                                 context:nil].size;
        
        point=CGPointMake(point.x+HorizontalInterverl+tmp.width, point.y);
        //为保证这种类型的按钮宽度与使用上面方法的保持一致。使用一行4（上面的方法的num一般使用值）个为基准，设立最小宽度。
        float minWidth=(viewWidth-HorizontalInterverl*4.0)/4.0;
        
        CGSize size={
            .width=needSize.width<(minWidth-10)?minWidth:needSize.width+10,
            .height=40,//改
        };
        
        tmp = size;
        
        if (point.x+HorizontalInterverl+size.width>viewWidth) {
            point=CGPointMake(HorizontalInterverl, point.y+VerticalInterverl+40);//改
            self.rowNum++;
        }
        if(point.x==HorizontalInterverl){
            point=CGPointMake(0, point.y);
        }
        
        AWOptionButton *button=[[AWOptionButton alloc] initWithFrame:CGRectMake(point.x,point.y, size.width, size.height)];
        
        [self.buttonMutableArray addObject:button];
        [self.buttonSelectedArray addObject:@0];
        // button.layer.borderWidth=0.5;
        button.titleLabel.font=[UIFont systemFontOfSize:13];
        [self setOptionButtonUnselected:button];//改
        [button setTitle:array[i] forState:UIControlStateNormal];
        
        button.tag=i;
        [button addTarget:self action:@selector(optionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        self.lastButtonFrame = button.frame;
        [self addSubview:button];
        self.sumHeight=CGRectGetMaxY(button.frame);
    }
}

- (void)addKeywordButtonClick {
    if ([self.delegate respondsToSelector:@selector(addKeywordButtonClick)]) {
        [self.delegate addKeywordButtonClick];
    }
}

//在最后添加一个button
- (void)addOneButtonWithTitle:(NSString *)string {
    CGPoint point=self.lastButtonFrame.origin;
    CGSize tmp = self.lastButtonFrame.size;
    
    CGSize needSize = [string boundingRectWithSize:CGSizeMake(SCR_W - 25, 27)//改
                                           options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                        attributes:@{
                                                     NSFontAttributeName: [UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor blackColor]
                                                     }
                                           context:nil].size;
    
    point=CGPointMake(point.x+HorizontalInterverl+tmp.width, point.y);
    //为保证这种类型的按钮宽度与使用上面方法的保持一致。使用一行4（上面的方法的num一般使用值）个为基准，设立最小宽度。
    float minWidth=(SCR_W - 25 -HorizontalInterverl*4.0)/4.0;
    
    CGSize size={
        .width=needSize.width<(minWidth-10)?minWidth:needSize.width+10,
        .height=40,//改
    };
    
    tmp = size;
    
    if (point.x+HorizontalInterverl+size.width>SCR_W-25) {
        point=CGPointMake(HorizontalInterverl, point.y+VerticalInterverl+40);//改
        self.rowNum++;
    }
    if(point.x==HorizontalInterverl){
        point=CGPointMake(0, point.y);
    }
    
    AWOptionButton *button=[[AWOptionButton alloc] initWithFrame:CGRectMake(point.x,point.y, size.width, size.height)];
    NSInteger buttonCount = self.buttonMutableArray.count;
    [self.buttonMutableArray insertObject:button atIndex:buttonCount-2];
    [self.buttonSelectedArray addObject:@0];
    // button.layer.borderWidth=0.5;
    button.titleLabel.font=[UIFont systemFontOfSize:13];
    [self setOptionButtonUnselected:button];//改
    [button setTitle:string forState:UIControlStateNormal];
    button.tag= self.buttonMutableArray.count - 2;
    [button addTarget:self action:@selector(optionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    self.lastButtonFrame = button.frame;
    
    AWOptionButton *addButton = [self.buttonMutableArray lastObject];
    if (self.lastButtonFrame.origin.x + self.lastButtonFrame.size.width + HorizontalInterverl + minWidth > SCR_W - 25) {
        addButton.frame = CGRectMake(0, self.lastButtonFrame.origin.y+VerticalInterverl+40, addButton.frame.size.width, addButton.frame.size.height);
    } else {
        addButton.frame = CGRectMake(self.lastButtonFrame.origin.x + self.lastButtonFrame.size.width +HorizontalInterverl, self.lastButtonFrame.origin.y, addButton.frame.size.width, addButton.frame.size.height);
    }
    self.sumHeight=CGRectGetMaxY(addButton.frame);
    
    if ([self.delegate respondsToSelector:@selector(changeKeywordViewHeight)]) {
        [self.delegate changeKeywordViewHeight];
    }
}

-(void) optionButtonClick:(id)sender{
    
    AWOptionButton *button=(AWOptionButton *)sender;
    if ([self.buttonSelectedArray[button.tag] isEqual:@0]) {
//        if (self.keyWordsArray.count == 5) {
//            [MBProgressHUD HUDShowMessage:@"关键字最多可以选择5个" addedToView:self];
//            return;
//        }
        [self setOptionButtonSelected:button];
        button.selected = YES;
    }else{
        [self setOptionButtonUnselected:button];
        button.selected = NO;
    };
    
    AWOptionButton *firstButton = self.buttonMutableArray[0];
    if ([button isEqual:firstButton] && firstButton.selected) {
        for (int i=1; i<[self.buttonMutableArray count]; i++) {
            AWOptionButton *tmpButton=self.buttonMutableArray[i];
            [self setOptionButtonUnselected:tmpButton];
        }
    } else {
        [self setOptionButtonUnselected:firstButton];
    }
//单选
    if (!self.multipleSelect) {
        for (int i=0; i<[self.buttonMutableArray count]; i++) {
            if (i!=button.tag) {
                AWOptionButton *tmpButton=self.buttonMutableArray[i];
                [self setOptionButtonUnselected:tmpButton];
            }
        }
    }
    
    NSString *keyWord ;
    NSMutableArray *keyWordsArr = [NSMutableArray array];
    for (UIButton *subButton in self.subviews) {
        if (subButton.selected == YES) {
            //            keyWord =  [globalGoodsCategoryGroup.dicsystem_keyword objectForKey: subButton.titleLabel.text];
            keyWord = subButton.titleLabel.text;
            [keyWordsArr addObject:keyWord];
        }
    }
    _keyWordsArray = keyWordsArr;
    
    //多选改为单选
    //    for (int i=0; i<[self.buttonMutableArray count]; i++) {
    //        if (i!=button.tag) {
    //            AWOptionButton *tmpButton=self.buttonMutableArray[i];
    //            [self setOptionButtonUnselected:tmpButton];
    //        }
    //    }
    //        if (button==self.buttonMutableArray[self.exclusiveButtonIndex]) {
    //            [self setOptionButtonSelected:self.buttonMutableArray[self.exclusiveButtonIndex]];
    //            for (int i=0; i<[self.buttonMutableArray count]; i++) {
    //                if (i!=self.exclusiveButtonIndex) {
    //                    AWOptionButton *tmpButton=self.buttonMutableArray[i];
    //                    [self setOptionButtonUnselected:tmpButton];
    //                }
    //            }
    //        }else{
    //            [self setOptionButtonUnselected:self.buttonMutableArray[self.exclusiveButtonIndex]];
    //        }
}

-(void) setOptionButtonSelected:(AWOptionButton *)button{
    //button.layer.borderColor=MAINCOLOR.CGColor;
    button.selectedImage.hidden=NO;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"发布2"] forState:UIControlStateNormal];
    button.backgroundColor = RED;
    
    self.buttonSelectedArray[button.tag]=@1;
}




-(void) setOptionButtonUnselected2:(AWOptionButton *)button{
    //button.layer.borderColor=[UIColor grayColor].CGColor;
    button.selectedImage.hidden=YES;
    [button setBackgroundImage:[UIImage imageNamed:@"technolofy_box"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.buttonSelectedArray[button.tag]=@0;
}


-(void) setOptionButtonUnselected:(AWOptionButton *)button{
    //button.layer.borderColor=[UIColor grayColor].CGColor;
    button.selected = NO;
    button.selectedImage.hidden=YES;
    [button setBackgroundImage:[UIImage imageNamed:@"发布3"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor whiteColor];
    self.buttonSelectedArray[button.tag]=@0;
}



-(void)setAllButtonsDisable{
    for (AWOptionButton *button in self.buttonMutableArray) {
        button.userInteractionEnabled=NO;
    }
}

-(void)resetAllButtons{
    for (AWOptionButton *button in self.buttonMutableArray) {
        [self setOptionButtonUnselected:button];
    }
}

-(void)removeSubviews{
    [self.subviews enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
        [obj removeFromSuperview];
    }];
}


@end
